import * as service from '../services';

export default {
  namespace: 'transaction',
  state: {
    data: {
      miner: {},
    },
  },
  reducers: {
    save(state, { payload: { data }}) {
      return { ...state, data };
    },
  },
  effects: {
    *fetch({ payload: { hash }}, {call, put}) {
      const result = yield call(service.getTransaction, hash);
      yield put({
        type: 'save',
        payload: {
          data: result.data,
        }
      });
    }
  },
  subscriptions: {
    setup({dispatch, history}) {
      return history.listen(({pathname}) => {
        if (pathname.startsWith('/transaction/')) {
          let paths = location.pathname.split('/');
          let hash = paths.pop();
          dispatch({
            type: 'fetch',
            payload: {hash}
          });
        }
      });
    },
  }
}
